package com.acwing.lc.part1;

import java.util.Arrays;

/**
 * @author rkc
 * @date 2022/9/17 9:52
 */
public class LC64最小路径和 {

    public int minPathSum(int[][] grid) {
        int n = grid.length, m = grid[0].length;
        int[][] f = new int[n][m];
        for (int i = 0; i < n; i++) Arrays.fill(f[i], 0x3f3f3f3f);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (i == 0 && j == 0) {
                    f[i][j] = grid[i][j];
                } else {
                    if (i != 0) f[i][j] = Math.min(f[i][j], f[i - 1][j] + grid[i][j]);
                    if (j != 0) f[i][j] = Math.min(f[i][j], f[i][j - 1] + grid[i][j]);
                }
            }
        }
        return f[n - 1][m - 1];
    }
}
